package com.qxy.practice.concurrent;

import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * @author : 戚欣扬
 * @Description :
 */
public class SemaphoreDemo {
    Semaphore semaphore = new Semaphore(1);

    public void stopCar(){
        try {
            semaphore.acquire();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(Thread.currentThread().getName()+" 抢到了车位 " );
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(Thread.currentThread().getName()+" 离开了商场" );
        semaphore.release();
    }

    public static void main(String[] args) {
        SemaphoreDemo semaphoreDemo = new SemaphoreDemo();
        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                semaphoreDemo.stopCar();
            },String.valueOf(i)).start();
        }
    }
}


